package Exercise;
//罗马数字的转换------方法一
import java.util.Scanner;
public class String_6 {
    public static void main(String[] args) {
        //用户输入
        Scanner sc = new Scanner(System.in);
        String sr;
        while(true){
        System.out.println("请输入一个长度小于等于9，并且全部为数字的字符串:");
        sr = sc.next();
        //合法性检验
        if(Judge(sr))
            break;
        }
        //转变罗马数字
        System.out.println(Trans(sr));
    }

    //检验
    public static boolean Judge(String sr){
        char[] s = {'0','1','2','3','4','5','6','7','8','9'};
        //对字符串长度验证
        int len = sr.length();
        if(len<=9){
            //对字符串的内容验证
            for (int i = 0; i < len; i++) {
                int flag=0 ;  //标记
                //比较，若字符串中出现非数字的，flag=0，跳出循环，返回false;全为数字，返回true
                for (int j = 0; j < s.length; j++) {
                    if(sr.charAt(i)==s[j]){
                        flag = 1;
                    }
                }
                if(flag==0){
                    System.out.println("内容错误，请重新输入！");
                    return false;
                }
            }
            return true;
        }else{
            System.out.println("长度错误，请重新输入！");
            return false;
        }
    }
    //转变
    public static String Trans(String sr){
        StringBuilder sb = new StringBuilder();
        String[] src = {"","I","II","III","IV","V","VI","VII","VII","IX"};
        for (int i = 0; i < sr.length(); i++) {
            int r = sr.charAt(i)-'0';
           sb.append(src[r]).append(" ");
        }
        String s = sb.toString();
        return s;
    }
}
